package com.siwen.result;

import java.util.List;
import java.util.Map;

public class Pager<T> {
    //总记录数
    private long totalRecords;
    //总页数
    private long totalPages;
    //每页显示数
    private int pageSize;
    //当前页
    private long curPage;
    //是否首页/末页
    private boolean firstPage = false;
    private boolean lastPage = false;
    //起始页码/结束页码
    private long startPage;
    private long endPage;
    //查询起始索引/结束索引
    private long startIndex;
    private long endIndex;
    //查询条件
    private Map<String, Object> condition;
    //页面数据
    private List<T> datas;

    public Pager(long totalRecords, Integer pageSize, Long curPage) {
        this.totalRecords = totalRecords;
        //赋值页面显示数
        if (pageSize == null || pageSize < 1) {
            this.pageSize = 10;
        } else {
            this.pageSize = pageSize;
        }
        //计算总页数
        this.totalPages = this.totalRecords % this.pageSize == 0 ? this.totalRecords / this.pageSize :
                this.totalRecords / this.pageSize + 1;
        //赋值当前页
        if (curPage == null || curPage < 1) {
            this.curPage = 1;
        } else {
            this.curPage = curPage;
        }
        if (this.curPage > this.totalPages) {
            this.curPage = this.totalPages;
        }

        //计算起始索引和结束索引
        this.startIndex = (this.curPage - 1) * this.pageSize;
        this.endIndex = this.startIndex + this.pageSize;

        //是否首页/末页
        if (this.curPage == 1) {
            this.firstPage = true;
        }
        if (this.curPage == this.totalPages) {
            this.lastPage = true;
        }

        //起始页码/结束页码
        if (this.totalPages <= 10) {
            this.startPage = 1;
            this.endPage = this.totalPages;
        } else {
            //根据当前页的变化 变化显示的页码
            if (this.curPage <= 6) {
                this.startPage = 1;
                this.endPage = 10;
            } else {
                this.startPage = this.curPage - 5;
                this.endPage = this.curPage + 4;
                if(this.endPage > this.totalPages ){
                    this.endPage = this.totalPages;
                }
            }
        }
    }

    public long getTotalRecords() {
        return totalRecords;
    }

    public long getTotalPages() {
        return totalPages;
    }
    public int getPageSize() {
        return pageSize;
    }

    public long getCurPage() {
        return curPage;
    }

    public boolean isFirstPage() {
        return firstPage;
    }

    public boolean isLastPage() {
        return lastPage;
    }

    public long getStartPage() {
        return startPage;
    }

    public long getEndPage() {
        return endPage;
    }

    public long getStartIndex() {
        return startIndex;
    }

    public long getEndIndex() {
        return endIndex;
    }

    public Map<String, Object> getCondition() {
        return condition;
    }

    public List<T> getDatas() {
        return datas;
    }

    public void setCondition(Map<String, Object> condition) {
        this.condition = condition;
    }

    public void setDatas(List<T> datas) {
        this.datas = datas;
    }
}
